package seatiger.tomcat.naming.resources;

import javax.naming.*;
import javax.naming.directory.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

/**
 *
 */
public class FileDirContext extends BaseDirContext{

    /**
     * 项目部署的文件夹
     */
    private File base ;

    private Hashtable env;

    public FileDirContext(Hashtable env){
        super(env);
    }

    public FileDirContext(){

    }

    /**
     * 设置项目目录
     * @param docBase
     */
    @Override
    public void setDocBase(String docBase) {
        if (docBase == null){
            throw new IllegalArgumentException("resources.null");
        }
        base = new File(docBase);
        try {
            base = base.getCanonicalFile();
        }catch (IOException e){

        }
        //验证文件夹是否存在，并合法
        if (!base.exists() || !base.isDirectory() || !base.canRead()){
            throw new IllegalArgumentException("fileResources.base");
        }
        super.setDocBase(docBase);
    }

    @Override
    public Object lookup(String name) throws NamingException {
        Object result = null;
        File file = file(name);
        if (file == null){
            throw new NamingException("resources.notFound");
        }
        if (file.isDirectory()){
            FileDirContext tempContext = new FileDirContext(env);
            tempContext.setDocBase(file.getPath());
            result = tempContext;
        }else{
            result = new FileResource(file);
        }
        return result;
    }



    private File file(String name) {
        File file = new File(base,name);
        if (file.exists() && file.canRead()){
            return file;
        }
        return null;
    }

    @Override
    public void bind(String name, Object obj) throws NamingException {

    }

    @Override
    public void rebind(String name, Object obj) throws NamingException {

    }

    @Override
    public void unbind(String name) throws NamingException {

    }

    @Override
    public void rename(String oldName, String newName) throws NamingException {

    }

    @Override
    public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
        return null;
    }

    @Override
    public NamingEnumeration<Binding> listBindings(String name) throws NamingException {
        return null;
    }

    @Override
    public void destroySubcontext(String name) throws NamingException {

    }

    @Override
    public Context createSubcontext(String name) throws NamingException {
        return null;
    }

    @Override
    public Object lookupLink(String name) throws NamingException {
        return null;
    }

    @Override
    public Hashtable<?, ?> getEnvironment() throws NamingException {
        return null;
    }

    @Override
    public String getNameInNamespace() throws NamingException {
        return null;
    }

    /**
     * 根据名称获取属性
     * @param name
     * @param attrIds
     * @return
     * @throws NamingException
     */
    @Override
    public Attributes getAttributes(String name, String[] attrIds) throws NamingException {
        File file = file(name);
        if (file == null){
            throw new NamingException("resources.notFound");
        }
        return new FileResourceAttributes(file);
    }

    @Override
    public void modifyAttributes(String name, int mod_op, Attributes attrs) throws NamingException {

    }

    @Override
    public void modifyAttributes(String name, ModificationItem[] mods) throws NamingException {

    }

    @Override
    public void bind(String name, Object obj, Attributes attrs) throws NamingException {

    }

    @Override
    public void rebind(String name, Object obj, Attributes attrs) throws NamingException {

    }

    @Override
    public DirContext createSubcontext(String name, Attributes attrs) throws NamingException {
        return null;
    }

    @Override
    public DirContext getSchema(String name) throws NamingException {
        return null;
    }

    @Override
    public DirContext getSchemaClassDefinition(String name) throws NamingException {
        return null;
    }

    @Override
    public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException {
        return null;
    }

    @Override
    public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes) throws NamingException {
        return null;
    }

    @Override
    public NamingEnumeration<SearchResult> search(Name name, String filter, SearchControls cons) throws NamingException {
        return null;
    }

    @Override
    public NamingEnumeration<SearchResult> search(String name, String filter, SearchControls cons) throws NamingException {
        return null;
    }

    @Override
    public NamingEnumeration<SearchResult> search(String name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException {
        return null;
    }

    private class FileResource extends Resource{
        /**
         * 关联的文件
         */
        private File file;
        public FileResource(File file){
            this.file = file;
        }

        @Override
        public InputStream streamContent() throws IOException {
            if (binaryContent == null){
                inputStream = new FileInputStream(file);
            }
            return super.streamContent();
        }
    }

    private class FileResourceAttributes extends ResourceAttributes{
        private File file;
        public FileResourceAttributes(File file){
            this.file = file;
        }

        @Override
        public long getContentLength() {
            if (contentLength != -1){
                return contentLength;
            }
            contentLength = file.length();
            return contentLength;
        }

        @Override
        public long getLastModified() {
            return lastModified != -1 ? lastModified : file.lastModified();
        }
    }
}
